<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-row :gutter="48">
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="状态">
                  <a-select style="max-width:100px" v-model="status" placeholder="请选择" @select="statusClick">
                    <a-select-option :value="item.value" v-for="(item,index) in optionStatus" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="12" :sm="24" style="padding-left: 10px;padding-right: 0px">
                <a-form-item label="归属地">
                  <a-select style="max-width:100px" v-model="province" placeholder="请选择" @select="typeClick">
                    <a-select-option :value="item.value" v-for="(item,index) in addressList" :key="index" >{{ item.label }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-range-picker
              :ranges="{ '今天': [moment(), moment()], '一月内': [moment(), moment().endOf('month')] }"
              @change="onChange"
            />
          </a-col>
          <a-col :md="8" :sm="24">
            <a-input-search placeholder="请输入用户昵称关键字" @search="onSearch" >
              <a-button type="primary" slot="enterButton" icon="search">搜索</a-button>
            </a-input-search>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :pagination="pagination"
    >
      <span slot="id" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="mobile" slot-scope="text">
        <ellipsis :length="12" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="userName" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="idCard" slot-scope="text">
        <ellipsis :length="15" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="mobileArea" slot-scope="text">
        <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="flag" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
      <span slot="createTime" slot-scope="text">
        <ellipsis :length="20" tooltip>{{ text }}</ellipsis>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
        </template>
      </span>
    </s-table>
    <edit-model ref="modal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import EditModel from './modules/EditModel'
import { userList } from '@/api/user'
import utils from '@/utils/common'
import addressJson from '@/viewsVue/city.json'
import { mapActions } from 'vuex'
import moment from 'moment'
var timeStamp = Number(new Date())

const statusMap = {
  0: {
    status: 'warning',
    text: '无效'
  },
  1: {
    status: 'success',
    text: '有效'
  }
}

export default {
  name: 'UserList',
  components: {
    STable,
    Ellipsis,
    EditModel
  },
  data () {
    return {
      status: '',
      optionStatus: [
        { value: '', label: '全部' },
        { value: '1', label: '有效' },
        { value: '0', label: '无效' }
      ],
      province: '',
      addressList: [
        { value: '', label: '全部' }
      ],
      // 查询参数
      queryParam: {},
      keyword: '',
      startTime: '',
      endTime: '',
      pagination: {
        current: 1,
        pageSize: 20
      },
      // 表头
      columns: [
        {
          title: 'ID',
          dataIndex: 'id',
          scopedSlots: { customRender: 'id' }
        },
        {
          title: '联系电话',
          dataIndex: 'mobile',
          scopedSlots: { customRender: 'mobile' }
        },
        {
          title: '昵称',
          dataIndex: 'userName',
          scopedSlots: { customRender: 'userName' }
        },
        {
          title: '身份证号',
          dataIndex: 'idCard',
          scopedSlots: { customRender: 'idCard' }
        },
        {
          title: '归属地',
          dataIndex: 'mobileArea',
          scopedSlots: { customRender: 'mobileArea' }
        },
        {
          title: '状态',
          dataIndex: 'flag',
          scopedSlots: { customRender: 'flag' }
        },
        {
          title: '注册时间',
          dataIndex: 'createTime',
          scopedSlots: { customRender: 'createTime' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '150px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: pagination => {
        return userList(Object.assign(pagination, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.queryParam = {
      token: utils.AuthToken(timeStamp, this.$store.getters.userKey),
      timestamp: timeStamp,
      adminId: this.$store.getters.adminId,
      keyword: this.keyword,
      province: this.province,
      status: this.status,
      startTime: this.startTime,
      endTime: this.endTime
    }
    var arr = []; var add = {}; var array = []
    arr = addressJson
    for (var i in arr) {
      if (arr[i][0].province.substr(arr[i][0].province.length - 1, 1) === '省' || arr[i][0].province.substr(arr[i][0].province.length - 1, 1) === '市') {
        add = {
          label: arr[i][0].province,
          value: arr[i][0].province.substr(0, arr[i][0].province.length - 1)
        }
      } else {
        add = {
          label: arr[i][0].province,
          value: arr[i][0].province
        }
      }
      array.push(add)
    }
    this.addressList = this.addressList.concat(array)
  },
  methods: {
    ...mapActions(['UserUpdate']),
    moment,
    statusClick (val) { // 状态
      this.queryParam.status = val
      this.$refs.table.refresh()
    },
    typeClick (val) { // 类型
      this.queryParam.province = val
      this.$refs.table.refresh()
    },
    onSearch (val) { // 搜索
      this.queryParam.keyword = val
      this.$refs.table.refresh()
    },
    onChange (dates, dateStrings) { // 日期
      this.queryParam.startTime = dateStrings[0]
      this.queryParam.endTime = dateStrings[1]
      this.$refs.table.refresh()
    },
    handleEdit (record) { // 编辑
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    ApiUpdate (record, index) { // 发布 下架
      var data = {
        id: record.id,
        status: index
      }
      this.UserUpdate(data)
        .then((res) => this.updateSuccess(res, record.id))
        .catch(err => this.requestFailed(err))
    },
    updateSuccess (res, key) {
      if (res.status === 200 || res.status === '200') {
        this.$notification['success']({
          message: '成功',
          description: res.message,
          duration: 4
        })
        this.$refs.table.refresh()
      } else {
        this.requestFailed(res)
      }
    },
    requestFailed (err) {
      this.$notification['error']({
        message: '错误',
        description: err.message || '请求出现错误，请稍后再试',
        duration: 4
      })
    }
  }
}
</script>
